package zhang.lao.api.controller.app;/**
 * Created by zhangqin on 2018/1/12.
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import zhang.lao.api.service.app.AppService;
import zhang.lao.build.tool.PathKit;
import zhang.lao.build.tool.RotateImage;
import zhang.lao.extents.spring.handle.ApiData;
import zhang.lao.pojo.api.req.ApiReqBody;
import zhang.lao.pojo.api.req.ApiReqData;
import zhang.lao.pojo.api.req.app.*;
import zhang.lao.pojo.api.resp.ApiRespData;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author
 * @create 2018-01-12 9:19
 **/
@CrossOrigin
@Controller
public class AppController {

    private static final Logger logger = LoggerFactory.getLogger(AppController.class);
    @Autowired
    private AppService appService;
    @RequestMapping(value = "/app/up/latlng",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData upLatLng(@ApiData ApiReqData<ApiUplatlngReq> apiReqData){
        return appService.uplatlng(apiReqData);
    }

    @RequestMapping(value ="/app/login",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData login(@ApiData ApiReqData<ApiLoginReq> apiReqData){
        return appService.login(apiReqData);
    }

    @RequestMapping(value ="/app/info/machine",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData machine(@ApiData ApiReqData<ApiMachineInfoReq> apiReqData){
        return appService.getMachineInfo(apiReqData);
    }

    @RequestMapping(value ="/app/info/machine/tool",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData machineTool(@ApiData ApiReqData<ApiMachineToolInfoReq> apiReqData){
        return appService.getMachineToolInfo(apiReqData);
    }

    @RequestMapping(value ="/app/sms/phonecode",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData getPhoneCode(@ApiData ApiReqData<ApiPhoneCodeReq> apiReqData){
        return  appService.getPhoneCode(apiReqData);
    }


    @RequestMapping(value ="/app/work/start",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData startWork(@ApiData ApiReqData<ApiWorkStartReq> apiReqData){
        return  appService.startWork(apiReqData);
    }

    @RequestMapping(value ="/app/work/finish",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData endWork(@ApiData ApiReqData<ApiWorkEndReq> apiReqData){
        return  appService.endWork(apiReqData);
    }

    @RequestMapping(value ="/app/work/delete",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData deleteWork(@ApiData ApiReqData<ApiWorkDelReq> apiReqData){
        return  appService.delWork(apiReqData);
    }
    @RequestMapping(value ="/app/work/running",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData runningWork(@ApiData ApiReqData<ApiReqBody> apiReqData){
        return  appService.runningWork(apiReqData);
    }

    @RequestMapping(value ="/app/work/info",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData infoWork(@ApiData ApiReqData<ApiWorkInfoReq> apiReqData){
        return  appService.infoWork(apiReqData);
    }
    @RequestMapping(value ="/app/work/list",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData workList(@ApiData ApiReqData<ApiWorkListReq> apiReqData){
        return  appService.workList(apiReqData);
    }

    @RequestMapping(value ="/app/work/trace",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData workTrace(@ApiData ApiReqData<ApiWorkInfoReq> apiReqData){
        return  appService.workTrace(apiReqData);
    }
    @RequestMapping(value ="/app/user/info/update",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData upDateUserInfo(@ApiData ApiReqData<ApiUpdateUserInfo> apiReqData){
        return  appService.upDateUserInfo(apiReqData);
    }

    @RequestMapping("/app/api/run@{degree}")
    public void getMachineImage(HttpServletResponse response, @PathVariable int degree){
        OutputStream outputStream=null;
        try {
            outputStream=response.getOutputStream();
            String path = PathKit.getWebRootPath()+ File.separator+"static"+ File.separator+"machine-icon.png";
            BufferedImage bufferedImage= ImageIO.read(new FileInputStream(path));
            ImageIO.write(RotateImage.rotateImage(bufferedImage,degree),"png",outputStream);
        } catch (IOException e) {
            logger.error(e.getMessage(),e);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        } finally{
            try {
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                logger.error(e.getMessage());
            }
        }
    }

    @RequestMapping(value ="/app/version/get",method = {RequestMethod.POST})
    public @ResponseBody ApiRespData appVersionGet(){
        return  ApiRespData.buildSucc("20180404");
    }

}
